package com.xiaoma.myaudience.biz.task;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

import com.xiaoma.myaudience.biz.model.HomeInfoModel;
import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.model.ModelUtils;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;

/**
 * 异步加载Home信息.
 * 
 */
public class HeadlineInfoTask extends BaseDataAsyncTask<Map<String,String>, Void, Map<String, Object>>{
    private final static String TAG = "HeadlineInfoTask";
    
    public static final String HEADLINES = "headlines";
    private Context mContext;
    public HeadlineInfoTask(Context context, int taskType, DataAsyncCallback callback) {
        super(context, taskType, callback);
        mContext = context;
    }


    @Override
    protected Map<String, Object> doInBackground(Map<String,String>... params) {  
    	Map<String, Object> result = null;
    	List<NameValuePair> value = new ArrayList<NameValuePair>();
    	if(params[0]!=null){
	    	value = new ArrayList<NameValuePair>();
	    	value.add(new BasicNameValuePair("type", params[0].get("type").toString()));
	    	value.add(new BasicNameValuePair("direction", "1"));
	    	value.add(new BasicNameValuePair("perpage", "10"));
	    	value.add(new BasicNameValuePair("line", params[0].get("line")));
    	}
        try {            //JSONObjectResult(getHttpClient(), Constant.URL_HOME, value, HttpUtils.GET)
        	JSONObject jsonObject = HttpUtils.getHttpJSONObjectResult(getHttpClient(), Constant.URL_HOME, value, HttpUtils.GET);
        	if (jsonObject != null) {
        		JSONObject obj = (JSONObject) jsonObject.get(HomeInfoModel.PARAM_DATA);
        		if (obj != null) {
        			result = ModelUtils.json2Map(obj);
				}
			}
        } catch (Exception e) {
            Logger.e(TAG, "doInBackground error!!!", e);
        } finally {
            closeHttpClient();
        }
        
        return result;
    }
}
